﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using xmlBookstore.classLibrary;

namespace xmlBookstore
{
    public partial class frmAddBook : Form
    {

        private DataRep dr;

        public frmAddBook(ref DataRep drincoming)
        {
            dr = drincoming;
            InitializeComponent();
            dataBind();
        }

        private void dataBind()
        {
            cbCategory.DataSource = null;
            cbCategory.Items.Clear();
            cbCategory.DataSource = dr.getCategoryList.Categories;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Category cat;
            cat = (Category)cbCategory.SelectedItem;
            // ako nisu uneseni svi podaci, ili ako nije odabrana kategorija te oba polja za novu kategoriju nisu ispunjena
            if (tbTitle.Text == "" || tbAuthor.Text == "" || tbAmount.Text == "" || tbPrice.Text == "" || tbYear.Text == "" || (cat.ToString() == ", " && (tbNewCat.Text == "" || tbNewType.Text == "")))
            {
                System.Windows.Forms.MessageBox.Show("You must enter all data", "Error");
            }
            // ako jesu, dodaj novu knjigu
            else
            {
                // ako su oba polja za novu kategoriju ispunjena
                if (tbNewCat.Text != "" && tbNewType.Text != "")
                {
                    // provjeri jel se ta upisana kategorija vec nalazi u dr
                    bool catAlreadyInDr = false;
                    foreach (Category catTest in dr.getCategoryList.Categories)
                        // ako se nalazi, pripremi ju za pridruzivanje knjizi
                        if (tbNewCat.Text + ", " + tbNewType.Text == catTest.ToString())
                        {
                            catAlreadyInDr = true;
                            cat = catTest;
                        }
                    // ako se ne nalazi, pripremi ju za pridruzivanje knjizi i dodaj ju u dr
                    if (catAlreadyInDr == false)
                    {
                        cat = new Category(tbNewCat.Text, tbNewType.Text);
                        dr.getCategoryList.Categories.Add(cat);
                    }

                }
                // u svakom slucaju, dodaj novu knjigu u dr
                int id = 0;
                foreach (Book book in dr.getInventory.Books) if (book.Id > id) id = book.Id;
                id++;
                try
                {
                    dr.getInventory.Books.Add(new Book(id, tbTitle.Text, tbAuthor.Text, Convert.ToInt32(tbYear.Text), cat, Convert.ToInt32(tbPrice.Text), Convert.ToInt32(tbAmount.Text)));
                    MessageBox.Show("Book successfuly added!");

                    // TODO: DODAT KOD ZA ACTUAL DODAVANJE KNJIGE U BAZU
                    dataBind();
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message);
                }


            }


        }
    }
}
